﻿using AutoMapper;
using Pay.DTO;
using Pay.Data;
using Pay.IService;
using Pay.Core;
using Pay.TO;
using System.Collections.Generic;

namespace Pay.Service.AutoMapper {

    public class ArticleMapper : IStartupDto {
        private IMapperConfigurationExpression _cfg;
        public ArticleMapper(IMapperConfigurationExpression cfg) {
            _cfg = cfg;
        }
        public void Execute() {
            Create();
        }
        protected virtual void Create() {
            _cfg.CreateMap<Article, article_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
                .ForMember(dest => dest.title, src => src.MapFrom(c => c.Title))
                .ForMember(dest => dest.img_url, src => src.MapFrom(c => c.ImgUrl))
                .ForMember(dest => dest.description, src => src.MapFrom(c => c.Description))
                .ForMember(dest => dest.status, src => src.MapFrom(c => c.Status))
                .ForMember(dest => dest.is_top, src => src.MapFrom(c => c.IsTop))
                .ForMember(dest => dest.is_highlight, src => src.MapFrom(c => c.IsHighLight))
                .ForMember(dest => dest.reply_num, src => src.MapFrom(c => c.ReplyNum))
                .ForMember(dest => dest.click_num, src => src.MapFrom(c => c.ClickNum))
                .ForMember(dest => dest.sort, src => src.MapFrom(c => c.Sort))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.merchant, src => src.MapFrom(c => c.Merchant))
                 .ForMember(dest => dest.update_time, src => src.MapFrom(c => c.UpdateTime.ToDateTime()))
                  .ForMember(dest => dest.type, src => src.MapFrom(c => c.Type))
                .ForMember(dest => dest.reply_articles, src => src.MapFrom(c => c.ReplyArticles));


            _cfg.CreateMap<article_dto, Article>()
                .ForMember(dest => dest.Id, src => src.MapFrom(c => c.id))
                .ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
                .ForMember(dest => dest.Title, src => src.MapFrom(c => c.title))
                .ForMember(dest => dest.ImgUrl, src => src.MapFrom(c => c.img_url))
                .ForMember(dest => dest.Description, src => src.MapFrom(c => c.description))
                .ForMember(dest => dest.Status, src => src.MapFrom(c => c.status))
                .ForMember(dest => dest.IsTop, src => src.MapFrom(c => c.is_top))
                .ForMember(dest => dest.IsHighLight, src => src.MapFrom(c => c.is_highlight))
                .ForMember(dest => dest.ReplyNum, src => src.MapFrom(c => c.reply_num))
                .ForMember(dest => dest.ClickNum, src => src.MapFrom(c => c.click_num))
                .ForMember(dest => dest.Sort, src => src.MapFrom(c => c.sort))
                .ForMember(dest => dest.CreateTime, src => src.MapFrom(c => c.create_time.ToUnix()))
                  .ForMember(dest => dest.UpdateTime, src => src.MapFrom(c => c.update_time.ToUnix()))
                     .ForMember(dest => dest.Type, src => src.MapFrom(c => c.type))
                .ForMember(dest => dest.Merchant, src => src.Ignore())
                .ForMember(dest => dest.ReplyArticles, src => src.Ignore());

            _cfg.CreateMap<ReplyArticle, reply_article_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.reply_content, src => src.MapFrom(c => c.ReplyContent))
                .ForMember(dest => dest.article_id, src => src.MapFrom(c => c.ArticleId))
                .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
                .ForMember(dest => dest.status , src => src.MapFrom(c => c.Status))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.merchant, src => src.MapFrom(c => c.Merchant));

            _cfg.CreateMap<reply_article_dto, ReplyArticle>()
                .ForMember(dest => dest.Id, src => src.MapFrom(c => c.id))
                .ForMember(dest => dest.ReplyContent, src => src.MapFrom(c => c.reply_content))
                .ForMember(dest => dest.ArticleId, src => src.MapFrom(c => c.article_id))
                .ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
                .ForMember(dest => dest.Status, src => src.MapFrom(c => c.status))
                .ForMember(dest => dest.CreateTime, src => src.MapFrom(c => c.create_time.ToUnix()))
                .ForMember(dest => dest.Merchant, src => src.Ignore());

            _cfg.CreateMap<Article, article_dto_ex>()
               .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
               .ForMember(dest => dest.title, src => src.MapFrom(c => c.Title))
               .ForMember(dest => dest.name, src => src.MapFrom(c => c.Merchant.Name))
               .ForMember(dest => dest.photo, src => src.MapFrom(c => c.Merchant.Photo))
               .ForMember(dest => dest.description, src => src.MapFrom(c => c.Description))
               .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
               .ForMember(dest => dest.reply_num, src => src.MapFrom(c => c.ReplyNum))
               .ForMember(dest => dest.click_num, src => src.MapFrom(c => c.ClickNum));

            _cfg.CreateMap<article_model, article_dto>()
               .ForMember(dest => dest.title, src => src.MapFrom(c => c.title))
               .ForMember(dest => dest.description, src => src.MapFrom(c => c.description))
               .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.merchant_id));

            _cfg.CreateMap<ReplyArticle, reply_article_ex>()
               .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
               .ForMember(dest => dest.name, src => src.MapFrom(c => c.Merchant.Name))
               .ForMember(dest => dest.photo, src => src.MapFrom(c => c.Merchant.Photo))
               .ForMember(dest => dest.reply_content, src => src.MapFrom(c => c.ReplyContent))
               .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
               .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()));

            _cfg.CreateMap<reply_article_model, reply_article_dto>()
               .ForMember(dest => dest.reply_content, src => src.MapFrom(c => c.reply_content))
               .ForMember(dest => dest.article_id, src => src.MapFrom(c => c.article_id))
               .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.merchant_id));

            _cfg.CreateMap<reply_article_dto, reply_article_ex>()
               .ForMember(dest => dest.reply_content, src => src.MapFrom(c => c.reply_content))
               .ForMember(dest => dest.id, src => src.MapFrom(c => c.id))
               .ForMember(dest => dest.name, src => src.MapFrom(c => c.merchant.name))
               .ForMember(dest => dest.photo, src => src.MapFrom(c => c.merchant.photo))
               .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.merchant_id))
               .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.create_time));

            _cfg.CreateMap<article_dto, article_dto_ex>()
               .ForMember(dest => dest.id, src => src.MapFrom(c => c.id))
               .ForMember(dest => dest.title, src => src.MapFrom(c => c.title))
               .ForMember(dest => dest.name, src => src.MapFrom(c => c.merchant.name))
               .ForMember(dest => dest.photo, src => src.MapFrom(c => c.merchant.photo))
               .ForMember(dest => dest.description, src => src.MapFrom(c => c.description))
               .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.create_time))
               .ForMember(dest => dest.reply_num, src => src.MapFrom(c => c.reply_num))
               .ForMember(dest => dest.click_num, src => src.MapFrom(c => c.click_num));
        }
    }
}
